﻿using GYCTOAManagementSystemNewApi.Model;
using GYCTOAManagementSystemNewApi.Model.Enum;
using GYCTOAManagementSystemNewApi.Model.ViewModel;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using static MongoDB.Driver.WriteConcern;

namespace GYCTOAManagementSystemNewApi.Tool
{
    public static class command
    {
        #region     GetAuditRule
        /// <summary>
        /// 根据用户id和模块编号，查找相应的审批规则
        /// </summary>
        /// <param name="sw">时间计时器</param>
        /// <param name="ALLUserList">获取所有人员信息</param>
        /// <param name="ALLAuditRuleList">获取所有审批流程</param>
        /// <param name="ModuleNumber">模块编号</param>
        /// <param name="SponsorUserid">数据对应用户id</param>
        /// <returns></returns>
        public static AuditRule GetAuditRule(Stopwatch sw, List<Users> ALLUserList, List<AuditRule> ALLAuditRuleList, string ModuleNumber, int SponsorUserid)
        {
            sw.Start();
            AuditRule auditrule = null;
            var user = ALLUserList.Where(x => x.Id == SponsorUserid).FirstOrDefault();
            if (user == null) return auditrule;
            List<AuditRule> auditRules = ALLAuditRuleList.Where(x => !x.Delete && x.ModuleNumber == ModuleNumber).ToList();
            foreach (var a in auditRules)
            {
                if (a.SponsorUser != null && a.SponsorUser.Split(',').Contains(SponsorUserid.ToString()))
                {
                    auditrule = a;
                    break;
                }
            }
            //if (auditrule == null)
            //{
            //    foreach (var a in auditRules)
            //    {
            //        if (a.SponsorDepart != null && a.SponsorDepart.Split(',').Contains(user.DepartmentId.ToString()))
            //        {
            //            auditrule = a;
            //            break;
            //        }
            //    }
            //}
            sw.Stop();
            return auditrule;
        }
        #endregion


        /// <summary>
        /// 获取枚举的描述信息
        /// </summary>
        /// <param name="en">枚举对象</param>
        /// <returns></returns>
        internal static string GetEnumDescription(this Enum en)
        {
            Type type = en.GetType();
            FieldInfo fd = type.GetField(en.ToString());
            if (fd == null) return string.Empty;
            object[] attrs = fd.GetCustomAttributes(typeof(DescriptionAttribute), false);
            string name = string.Empty;
            foreach (DescriptionAttribute attr in attrs)
            {
                name = attr.Description;
            }
            return name;
        }



        public static DateTime ToDate(this string dateString)
        {
            if (DateTime.TryParse(dateString, out DateTime date))
            {
                return date;    
            }
            else {
                return default;
            }
        }

        /// <summary>
        /// 文件大小转换
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public static string CalculateSize(this long size)
        {
            string length = string.Empty;

            if (size < 1024)
            {
                length = size + "bytes";
            }
            else
                if (size < 1024 * 1024)
            {
                length = float.Parse((size * 10 / 1024).ToString()) / 10 + "KB";
            }
            else
                    if (size < 1024 * 1024 * 1024)
            {
                length = float.Parse((size * 10 / 1048576).ToString()) / 10 + "MB";
            }
            else
            {
                length = float.Parse((size * 10 / 1073741824).ToString()) / 10 + "GB";
            }

            return length;


        }
         
        /// <summary>
        /// 获取文件格式
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public static string LastFileTypeName(this string  FileName)
        { 
            return FileName.Substring(FileName.LastIndexOf('.'), FileName.Length- FileName.LastIndexOf('.')).ToUpper();
        }



        /// <summary>
        /// 判定指定路径下文件或者目录是否存在
        /// </summary>
        /// <param name="Path"></param>
        /// <param name="fileEnum">是否文件</param>
        /// <returns></returns>
        public static bool FileDirectoryInfoExists(this string Path, KnowledgeFileEnum fileEnum = KnowledgeFileEnum.directory)
        { 
            if (fileEnum == KnowledgeFileEnum.directory)
            {
                return Directory.Exists(Path);
            }
            else
            {
                return File.Exists(Path);
            }
        }



        #region 金额转中文大写

       

        static string[] CN_CAPITALS = { "", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
        static string[] CN_UNITS = { "", "拾", "佰", "仟" };
        static string[] CN_GROUP_UNITS = { "", "万", "亿", "兆" };
        static string CN_DOLLAR = "元";
        static string CN_TEN_CENTS = "角";
        static string CN_CENT = "分";
        static string CN_INTEGER = "整";


        /// <summary>
        /// 金额转中文大写
        /// </summary>
        /// <param name="amount"></param>
        /// <returns></returns>
       public static string ConvertToChineseCapital(decimal amount)
        {
            if (amount == 0)
                return "零" + CN_DOLLAR + CN_INTEGER;

            StringBuilder result = new StringBuilder();
            long integerPart = (long)Math.Floor(amount);
            int decimalPart = (int)Math.Round((amount - integerPart) * 100);

            // 处理整数部分
            int groupCount = 0;
            while (integerPart > 0)
            {
                int group = (int)(integerPart % 10000);
                bool needZero = false;
                StringBuilder groupResult = new StringBuilder();
                for (int i = 0; i < 4; i++)
                {
                    int digit = group % 10;
                    if (digit != 0)
                    {
                        groupResult.Insert(0, CN_UNITS[i]);
                        groupResult.Insert(0, CN_CAPITALS[digit]);
                        needZero = false;
                    }
                    else
                    {
                        if (!needZero)
                        {
                            groupResult.Insert(0, CN_CAPITALS[0]);
                            needZero = true;
                        }
                    }
                    group /= 10;
                }
                groupResult.Append(CN_GROUP_UNITS[groupCount]);
                result.Insert(0, groupResult);
                integerPart /= 10000;
                groupCount++;
            }

            // 处理小数部分
            if (decimalPart > 0)
            {
                result.Append(CN_DOLLAR);
                int tenCentsDigit = decimalPart / 10;
                int centsDigit = decimalPart % 10;
                if (tenCentsDigit != 0)
                    result.Append(CN_CAPITALS[tenCentsDigit] + CN_TEN_CENTS);
                if (centsDigit != 0)
                    result.Append(CN_CAPITALS[centsDigit] + CN_CENT);
            }
            else
            {
                result.Append(CN_DOLLAR + CN_INTEGER);
            }
            return result.ToString();
        }



        /// <summary>
        /// 获取枚举描述内容
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDescription(this Enum value)
        {
            Type type = value.GetType();
            MemberInfo[] memInfo = type.GetMember(value.ToString());
            if (memInfo != null && memInfo.Length > 0)
            {
                object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attrs != null && attrs.Length > 0)
                {
                    return ((DescriptionAttribute)attrs[0]).Description;
                }
            }
            return value.ToString();
        }







        #endregion
    }
}
